#!/user/bin/env python3
# -*- coding: utf-8 -*-
"""
--------------------------------------
    Author:     JiChao_Song
    Date  :     2020/12/22 10:32
    Desc  :
--------------------------------------
"""
import requests
from flask import json


class SimpleRequesUtils:

    def __init__(self, methods: str, url: str, *args, **kwargs):
        """

        :param methods: 请求方法
        """
        self.url = url
        self.methods = methods
        self.data = kwargs.get('data')
        self.params = kwargs.get('params')
        self.headers = kwargs.get('headers')
        self.res = None

    def dictResult(self):
        return {'responseBody': self.res.json() if self.res.json() else self.res.text,
                'requestBody': self.res.request.body, 'requestHeader': dict(self.res.request.headers),
                'spendTime': self.res.elapsed.total_seconds(), 'respinseHeader': dict(self.res.headers),
                'requestMethod': self.res.request.method,
                }

    def post(self):
        self.res =  requests.post(url = self.url, data = self.data, headers = self.headers)
        return self.res

    def get(self):
        self.res =  requests.get(url = self.url, params = self.params, headers = self.headers)
        return self.res

    def send_requests(self):
        """
        发送请求
        :return:
        """

        if self.methods == 'POST':
            return self.post()

        if self.methods == 'GET':
            return self.get()
